using System.Globalization;
using System.Windows.Data;

namespace Teach1.Gui.Models;

public class RadioButtonToIntConverter: IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        // Convert from integer to boolean for RadioButton IsChecked
        if (value is int intValue && parameter is string { } parameterString)
        {
            return int.TryParse(parameterString, out var parameterValue) && intValue == parameterValue;
        }
        return false;
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        // Convert from RadioButton IsChecked to integer
        if (value is true && parameter is string { } parameterString)
        {
            return int.TryParse(parameterString, out var parameterValue) ? parameterValue : Binding.DoNothing;
        }
        return Binding.DoNothing;
    }
}